package com.atguigu.tingshu.order.strategy.impl;

import com.atguigu.tingshu.album.client.AlbumFeignClient;
import com.atguigu.tingshu.common.constant.SystemConstant;
import com.atguigu.tingshu.common.utils.ThrowUtil;
import com.atguigu.tingshu.model.album.AlbumInfo;
import com.atguigu.tingshu.order.strategy.OrderBuildStrategy;
import com.atguigu.tingshu.user.client.UserFeignClient;
import com.atguigu.tingshu.vo.order.OrderDerateVo;
import com.atguigu.tingshu.vo.order.OrderDetailVo;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

/**
 * 专辑订单构建策略
 */
@Slf4j
@Component
public class AlbumOrderBuildStrategy implements OrderBuildStrategy {

    @Autowired
    private AlbumFeignClient albumFeignClient;

    @Autowired
    private UserFeignClient userFeignClient;

    @Override
    public OrderInfoVo buildOrderInfo(TradeVo tradeVo, Long userId) {
        Long albumId = tradeVo.getItemId();
        log.info("构建专辑订单信息 - userId: {}, albumId: {}", userId, albumId);

        // 1. 查询专辑信息
        AlbumInfo albumInfo = albumFeignClient.getAlbumInfo(albumId).getData();
        ThrowUtil.throwIf(albumInfo == null, "专辑不存在");

        // 2. 判断用户是否已购买过该专辑
        Boolean isPaidAlbum = userFeignClient.isPaidAlbum(albumId).getData();
        ThrowUtil.throwIf(isPaidAlbum != null && isPaidAlbum, "您已购买过该专辑，无需重复购买");

        // 3. 查询用户信息，判断是否为VIP
        UserInfoVo userInfoVo = userFeignClient.getUserInfoVo(userId).getData();
        boolean isVip = userInfoVo != null && userInfoVo.getIsVip() != null && userInfoVo.getIsVip() == 1;
        log.info("用户VIP状态 - userId: {}, isVip: {}", userId, isVip);

        // 4. 构建OrderInfoVo
        OrderInfoVo orderInfoVo = new OrderInfoVo();
        orderInfoVo.setItemType(SystemConstant.ORDER_ITEM_TYPE_ALBUM);

        // 5. 构建专辑订单明细
        OrderDetailVo orderDetailVo = new OrderDetailVo();
        orderDetailVo.setItemId(albumInfo.getId());
        orderDetailVo.setItemName(albumInfo.getAlbumTitle());
        orderDetailVo.setItemUrl(albumInfo.getCoverUrl());
        orderDetailVo.setItemPrice(albumInfo.getPrice());

        List<OrderDetailVo> orderDetailVoList = new ArrayList<>();
        orderDetailVoList.add(orderDetailVo);
        orderInfoVo.setOrderDetailVoList(orderDetailVoList);

        // 6. 计算订单金额
        BigDecimal originalAmount = albumInfo.getPrice();
        BigDecimal discountRate = isVip ? albumInfo.getVipDiscount() : albumInfo.getDiscount();

        BigDecimal orderAmount;
        if (discountRate != null && discountRate.compareTo(new BigDecimal("-1")) != 0) {
            orderAmount = originalAmount.multiply(discountRate).setScale(2, RoundingMode.HALF_UP);
        } else {
            orderAmount = originalAmount;
        }

        BigDecimal derateAmount = originalAmount.subtract(orderAmount);
        orderInfoVo.setOriginalAmount(originalAmount);
        orderInfoVo.setDerateAmount(derateAmount);
        orderInfoVo.setOrderAmount(orderAmount);

        // 7. 订单减免明细列表
        List<OrderDerateVo> orderDerateVoList = new ArrayList<>();
        if (derateAmount.compareTo(BigDecimal.ZERO) > 0) {
            OrderDerateVo orderDerateVo = new OrderDerateVo();
            orderDerateVo.setDerateType(SystemConstant.ORDER_DERATE_ALBUM_DISCOUNT);
            orderDerateVo.setDerateAmount(derateAmount);
            orderDerateVo.setRemarks(isVip ? "VIP专享折扣" : "专辑折扣");
            orderDerateVoList.add(orderDerateVo);
        }
        orderInfoVo.setOrderDerateVoList(orderDerateVoList);

        log.info("专辑订单信息构建完成 - albumId: {}, isVip: {}, orderAmount: {}", albumId, isVip, orderAmount);
        return orderInfoVo;
    }

    @Override
    public String getOrderType() {
        return SystemConstant.ORDER_ITEM_TYPE_ALBUM;
    }
}
